package com.quanyan.user.service.impl;


import com.quanyan.club.entity.RespClubActivity;
import com.quanyan.club.service.ClubOpenService;
import com.quanyan.comment.reqeust.ReqUserCollectQuery;
import com.quanyan.comment.response.RespUserCollect;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.constants.Constants;
import com.quanyan.travel.api.RespOutdoorActivity;
import com.quanyan.travel.service.TravelOpenService;
import com.quanyan.user.constants.UserConstants;
import com.quanyan.user.model.vo.response.RespActivityCollect;
import com.quanyan.user.service.ActivityCollectService;
import com.quanyan.user.service.UserCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by Administrator on 2016/7/12.
 */
@Service
public class ActivityCollectServiceImpl implements ActivityCollectService {

    @Autowired
    private UserCollectService userCollectService;

   @Autowired
    private ClubOpenService clubOpenService;

   @Autowired
    private TravelOpenService travelOpenService;




    @Override
    public PageObj<List<RespActivityCollect>> queryActivityCollect(Integer uid,Integer pageNumber,Integer pageSize) {
        ReqUserCollectQuery reqUserCollectQuery = new ReqUserCollectQuery();
        reqUserCollectQuery.setUserId(uid);
        reqUserCollectQuery.setPageNum(pageNumber);
        reqUserCollectQuery.setPageSize(pageSize);
        PageObj<List<RespUserCollect>> lst = userCollectService.queryActivityCollect(reqUserCollectQuery);
        List<RespActivityCollect>  respActivityCollectList = new ArrayList<RespActivityCollect>();
        if(lst.getTotal() == 0)
            return PageObj.create(lst.getTotal(), lst.getPageNum(), lst.getPageCount(), respActivityCollectList);

        List<Integer> clubList = new ArrayList<Integer>();//俱乐部互动
        List<Integer> outdoorList = new ArrayList<Integer>();//户外活动

        for(RespUserCollect r :lst.getItems()){
            if(Constants.CLUB_COLLECT_ACTIVITY_BUSINESS_CODE.equals(r.getBusinessCode()))
                clubList.add(Integer.parseInt(r.getBusinessId()));
            else if(Constants.BUSINESS_OUTDOOR_CM.equals(r.getBusinessCode()))
                outdoorList.add(Integer.parseInt(r.getBusinessId()));
        }

        if(clubList!=null && clubList.size()>0){
            List<RespClubActivity> clubActivity = clubOpenService.batchQueryActivityList(clubList);
            for(RespClubActivity a:clubActivity){
                RespActivityCollect respActivityCollect = new RespActivityCollect();
                respActivityCollect.setActivityId(a.getId());
                respActivityCollect.setActivityName(a.getName());
                respActivityCollect.setActivityType(UserConstants.ACTIVITY_TYPE_CLUB);
                respActivityCollect.setActivityDate(a.getStartTime().getTime());
                if(a.getPrice() != null)
                    respActivityCollect.setPrice(a.getPrice().doubleValue());

                if(a.getRespActivityPoster() != null)
                    respActivityCollect.setLogo(a.getRespActivityPoster().getUrl());

                if(a.getPlaceDubboProvider() != null)
                    respActivityCollect.setArea(a.getPlaceDubboProvider().getName());
                else
                    respActivityCollect.setArea(a.getPlaceAddress());

                respActivityCollect.setClubActivityType(a.getActivityType());
                respActivityCollectList.add(respActivityCollect);
            }
        }
       if(outdoorList != null && outdoorList.size() > 0){
           List<RespOutdoorActivity> outdoorActivityList = travelOpenService.getOutdoorActivityListByids(outdoorList);
           for (RespOutdoorActivity r : outdoorActivityList){
               RespActivityCollect respActivityCollect = new RespActivityCollect();
               respActivityCollect.setActivityId(r.getId());
               respActivityCollect.setActivityName(r.getName());
               if(r.getPrice() != null)
                    respActivityCollect.setPrice(r.getPrice().doubleValue());
               respActivityCollect.setArea(r.getAddress());
               respActivityCollect.setActivityType(UserConstants.ACTIVITY_TYPE_OUTDOOR);
               respActivityCollect.setLogo(r.getImageUrl());
               if(r.getStartTime() != null)
                    respActivityCollect.setActivityDate(r.getStartTime().getTime());
               respActivityCollectList.add(respActivityCollect);
           }
       }

        Comparator<RespActivityCollect> comparator = new Comparator<RespActivityCollect>() {
            @Override
            public int compare(RespActivityCollect o1, RespActivityCollect o2) {
                return o2.getActivityDate().compareTo(o1.getActivityDate());
            }
        };
        Collections.sort(respActivityCollectList, comparator);
        PageObj<List<RespActivityCollect>>  result = PageObj.create(lst.getTotal(), lst.getPageNum(), lst.getPageCount(), respActivityCollectList);

        return result;
    }
}
